﻿using System.Collections.Concurrent;
using System.Drawing;
using System.Windows;
using System.Windows.Media.Imaging;

namespace PLCSharp.CoreModel.Tools
{
    public class WpfTool
    {
        private readonly ConcurrentQueue<PointF> PointsQueue = new();
        readonly List<PointF> Points = [];
        public async Task ExecuteDrawAsync(WriteableBitmap _imgSource)
        {
            while (true)
            {
                await Task.Delay(100);
                if (PointsQueue.IsEmpty) continue;

                while (!PointsQueue.IsEmpty)
                {
                    PointsQueue.TryDequeue(out PointF point);
                    Points.Add(point);
                }
                Application.Current?.Dispatcher.Invoke(delegate
                {
                    WriteableBitmap writeableBitmap = _imgSource;
                    int width = (int)writeableBitmap.Width;
                    int height = (int)writeableBitmap.Height;
                    writeableBitmap.Lock();
                    Graphics graphics = Graphics.FromImage(new Bitmap(width, height, writeableBitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, writeableBitmap.BackBuffer));
                    graphics.Clear(Color.Black);
                    //System.Drawing.FontFamily family = new("微软雅黑");

                    //求最小点，看是否为负；为负则整体偏移
                    var min_x = Points.Min(p => p.X);
                    var min_y = Points.Min(p => p.Y);

                    var points = Points.ToArray();
                    if (min_x < 0)
                        for (int i = 0; i < Points.Count; i++)
                        {
                            points[i] = new PointF(Points[i].X - min_x, Points[i].Y);
                        }
                    if (min_y < 0)
                        for (int i = 0; i < Points.Count; i++)
                        {
                            points[i] = new(Points[i].X, Points[i].Y - min_y);
                        }
                    for (int i = 0; i < Points.Count; i++)
                    {
                        points[i] = new(points[i].X + 100, points[i].Y + 100);
                    }
                    var max_x = points.Max(p => p.X);
                    var max_y = points.Max(p => p.Y);
                    var scalex = max_x / width;
                    var scaley = max_y / height;
                    var scale = Math.Max(scaley, scalex);
                    foreach (var point in points)
                    {

                        graphics.DrawRectangle(new Pen(Color.White), new Rectangle((int)(point.X / scale), (int)(point.Y / scale), 1, 1));

                    }

                    graphics.Flush();
                    writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
                    writeableBitmap.Unlock();

                });
            }
        }

        public static System.Windows.Media.Color GetRandColor(int start = 0, int end = 256)
        {
            // 创建一个Random对象
            Random random = new Random();

            // 生成一个0到255之间的随机整数作为红色分量
            byte red = Convert.ToByte(random.Next(start, end));

            // 生成一个0到255之间的随机整数作为绿色分量
            byte green = Convert.ToByte(random.Next(start, end));

            // 生成一个0到255之间的随机整数作为蓝色分量
            byte blue = Convert.ToByte(random.Next(start, end));

            // 将RGB值转换为ARGB格式的颜色值
            System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(255, red, green, blue);

            return color;

        }

        public static System.Windows.Media.Brush GetRandBrush(int start = 0, int end = 256)
        {
            return new System.Windows.Media.SolidColorBrush(GetRandColor(start, end));
        }
    }
}
